//
//  ALCacheData.m
//  Amily
//
//  Created by 张鹏 on 16/2/2.
//  Copyright © 2016年 Amily. All rights reserved.
//

#import "ALCacheData.h"

@implementation ALCacheData

+ (void)writeData:(id)data file:(NSString *)file plist:(NSString *)plist
{
    [self createPlist:file];
    [data writeToFile:[self plistPath:file plist:plist] atomically:YES];
}

+ (id)objectForPlistKey:(NSString *)key file:(NSString *)filePath plist:(NSString *)plistPath
{

    NSDictionary *plist =[NSDictionary dictionaryWithContentsOfFile:[self plistPath:filePath plist:plistPath]];
    return  [NSString isVaildString:key] ? plist[key]:plist;
//    return  plist[key];

}

+ (void)set:(id)value key:(NSString *)key sub:(NSString *)subkey file:(NSString *)filePath plist:(NSString *)plistPath
{
    NSMutableDictionary *dict =[NSMutableDictionary dictionaryWithContentsOfFile:[self plistPath:filePath plist:plistPath]];

    if([NSString isVaildString:subkey])
    {
        NSMutableDictionary *subDict =  dict[subkey];
        [subDict setObject:value forKey:key];
        [dict setObject:subDict forKey:subkey];
    }else{
        [dict setObject:value forKey:key];
    }

    [self writeData:dict file:filePath plist:plistPath];
}

+ (BOOL)isExists:(NSString *)filePath
{
    NSFileManager *file = [NSFileManager defaultManager];
    return [file fileExistsAtPath:[self loginDataListPath:filePath]];
}

+ (void)createPlist:(NSString *)filePath
{
    NSFileManager *file = [NSFileManager defaultManager];
    if (![self isExists:filePath]) {
        [file createDirectoryAtPath:[self loginDataListPath:filePath] withIntermediateDirectories:YES attributes:nil error:nil];
    }
}

+ (NSString *)plistPath:(NSString *)file plist:(NSString *)plist
{
    return [[self loginDataListPath:file] stringByAppendingPathComponent:plist];
}

+ (NSString *)loginDataListPath:(NSString *)filePath{
    
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    return [documentPath stringByAppendingPathComponent:filePath];
}

+ (void)removeFile:(NSString *)filePath
{
    NSFileManager *file = [NSFileManager defaultManager];
    if ([self isExists:filePath]) {
        [file removeItemAtPath:[self loginDataListPath:filePath] error:nil];
    }
}


@end
